import Vue from 'vue'
import axios from 'axios'
import { Notify } from 'quasar'
import {getToken} from "src/lib/Account";

axios.create({
  baseURL: 'http://localhost:10708'
});

axios.interceptors.request.use((config) => {
  let token = getToken('access')
  if(token) config.headers.common.Authorization = token;
  return config;
})

axios.interceptors.response.use((res) => {
  if (!res.data.status) Notify.create({
    message: 'Error',
    caption: res.data.error,
    icon: 'clear'
  })
  else return res.data.data || true;
}, error => {
  console.log(error);
  let code = error.response.statusCode;
  if (code === 401) {
    window.location.replace('/account/login');
  }
  Notify.create({
    message: 'Error',
    caption: error.message,
    icon: 'clear'
  })
})

Vue.prototype.$axios = axios
